package kzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kzy.dto.DishDto;
import kzy.entiy.Dish;
import kzy.entiy.DishFlavor;
import kzy.entiy.Employee;
import kzy.mapper.DishMapper;
import kzy.service.DishFlavorService;
import kzy.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isNotEmpty;

/**
 * @Author 大溪地
 * @create 2023年05月26日22:14
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

@Autowired
DishFlavorService dishFlavorService;

	@Override
	public void saveWithFlavor(DishDto dishDto) {
//		保存数据
		super.save(dishDto);

		Long id = dishDto.getId();
//		dishFlavorService.

		List<DishFlavor> flavors= dishDto.getFlavors();
		for (int i = 0; i < flavors.size(); i++) {
			flavors.get(i).setDishId(id);
		}
			dishFlavorService.saveBatch(flavors);

	}


	/**
	 * 更新菜品数据  先删除味道然后再添加味道
	 * @param dish
	 */
	@Override
	@Transactional
	public void updateDish(Dish dish) {
//		根据id更新数据
		super.updateById(dish);

		Long id = dish.getId();
		DishFlavor dishFlavor=new DishFlavor();
		dishFlavor.setDishId(id);
//		先删除味道
		dishFlavorService.updateDish(dishFlavor);
//		获得味道
		List<DishFlavor> flavors= dish.getFlavors();
		for (int i = 0; i < flavors.size(); i++) {
			flavors.get(i).setDishId(id);
		}
//		再添加味道
		dishFlavorService.saveBatch(flavors);
	}
}
